﻿using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.Json;

namespace IdPhotoAPIClient
{
    public class IdPhotoDemo
    {
        private static string _timeString = DateTime.Now.ToString("yyyyMMdd_HHmmssSSS");

        public static async Task Test()
        {
            Console.WriteLine("证件照API客户端示例程序");

            _timeString = DateTime.Now.ToString("yyyyMMdd_HHmmssSSS");

            // 创建API客户端实例
            var client = new IdPhotoApiClient("http://localhost:8080");

            try
            {
                // 示例：生成证件照(底透明)
                await DemoIdPhoto(client);

                // 示例：添加背景色
                await DemoAddBackground(client);

                // 示例：生成六寸排版照
                await DemoGenerateLayoutPhotos(client);

                // 示例：人像抠图
                await DemoHumanMatting(client);

                // 示例：图像加水印
                await DemoWatermark(client);

                // 示例：设置图像KB大小
                await DemoSetKb(client);

                // 示例：证件照裁切
                await DemoIdPhotoCrop(client);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
            }
            finally
            {
                client.Dispose();
            }

            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }

        static async Task DemoIdPhoto(IdPhotoApiClient client)
        {
            Console.WriteLine("\n=== 1. 生成证件照(底透明) ===");
            try
            {
                var imagePath = "test.jpg"; // 替换为实际的图片路径
                if (File.Exists(imagePath))
                {
                    var result = await client.IdPhotoAsync(imagePath, 413, 295);
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64_standard != null)
                    {

                        // 保存结果图片
                        var base64String = result.image_base64_standard;
                        await SaveBase64Image(base64String, _timeString + "_standard.jpg");
                        if (!string.IsNullOrEmpty(result.image_base64_hd))
                        {
                            var base64hd = result.image_base64_hd;
                            await SaveBase64Image(base64hd, _timeString + "_hd.jpg");
                        }
                        Console.WriteLine("证件照生成成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"生成证件照失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 添加背景色
        /// </summary>
        static async Task DemoAddBackground(IdPhotoApiClient client)
        {
            Console.WriteLine("\n=== 2. 添加背景色 ===");
            try
            {
                var imagePath = _timeString + "_standard.jpg"; // 使用之前生成的透明底图片
                if (File.Exists(imagePath))
                {
                    var result = await client.AddBackgroundAsync(imagePath, "638cce");
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        var base64hd = result.image_base64;
                        await SaveBase64Image(base64hd, _timeString + "_with_background.jpg");
                        Console.WriteLine("添加背景色成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"添加背景色失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 生成六寸排版照
        /// </summary>
        static async Task DemoGenerateLayoutPhotos(IdPhotoApiClient client)
        {
            Console.WriteLine("\n=== 3. 生成六寸排版照 ===");
            try
            {
                var imagePath = _timeString + "_with_background.jpg"; // 使用之前生成的带背景图片
                if (File.Exists(imagePath))
                {
                    var result = await client.GenerateLayoutPhotosAsync(imagePath, 413, 295);
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        await SaveBase64Image(result.image_base64, _timeString + "_layout.jpg");
                        Console.WriteLine("六寸排版照生成成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"生成六寸排版照失败: {ex.Message}");
            }
        }

        static async Task DemoHumanMatting(IdPhotoApiClient client)
        {
            Console.WriteLine("\n=== 4. 人像抠图 ===");
            try
            {
                var imagePath = "test.jpg"; // 替换为实际的图片路径
                if (File.Exists(imagePath))
                {
                    var result = await client.HumanMattingAsync(imagePath);
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        await SaveBase64Image(result.image_base64, _timeString + "_matting.png");
                        Console.WriteLine("人像抠图成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"人像抠图失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 图像加水印
        /// </summary>
        static async Task DemoWatermark(IdPhotoApiClient client)
        {
            Console.WriteLine("\n=== 5. 图像加水印 ===");
            try
            {
                var imagePath = "test.jpg"; // 替换为实际的图片路径
                if (File.Exists(imagePath))
                {
                    var result = await client.WatermarkAsync(imagePath, "Test Photo");
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        await SaveBase64Image(result.image_base64, _timeString + "watermarked.jpg");
                        Console.WriteLine("添加水印成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"添加水印失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 设置图像KB大小
        /// </summary>
        static async Task DemoSetKb(IdPhotoApiClient client)
        {
            Console.WriteLine("\n=== 6. 设置图像KB大小 ===");
            try
            {
                var imagePath = "test.jpg"; // 替换为实际的图片路径
                if (File.Exists(imagePath))
                {
                    var result = await client.SetKbAsync(imagePath, 50); // 设置为50KB
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        await SaveBase64Image(result.image_base64, "kb_adjusted_image.jpg");
                        Console.WriteLine("设置图像KB大小成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"设置图像KB大小失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 证件照裁切
        /// </summary>
        static async Task DemoIdPhotoCrop(IdPhotoApiClient client)
        {
            Console.WriteLine("\n=== 7. 证件照裁切 ===");
            try
            {
                var imagePath = "idphoto_matting.png"; // 替换为实际的透明底图片路径
                if (File.Exists(imagePath))
                {
                    var result = await client.IdPhotoCropAsync(imagePath, 413, 295);
                    Console.WriteLine($"状态: {result.status}");
                    if (result.status && result.image_base64 != null)
                    {
                        await SaveBase64Image(result.image_base64, "cropped_idphoto.jpg");
                        if (!string.IsNullOrEmpty(result.image_base64_hd))
                        {
                            await SaveBase64Image(result.image_base64_hd, "cropped_idphoto_hd.jpg");
                        }
                        Console.WriteLine("证件照裁切成功，已保存到本地文件。");
                    }
                }
                else
                {
                    Console.WriteLine($"跳过测试：未找到测试图片 {imagePath}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"证件照裁切失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 把Base64字符串保存为图片文件
        /// </summary>
        /// <param name="base64String"></param>
        /// <param name="fileName"></param>
        static async Task SaveBase64Image(string? base64String, string fileName)
        {
            if (string.IsNullOrEmpty(base64String)) return;

            var imageBytes = Convert.FromBase64String(base64String.Replace("data:image/png;base64,", ""));
            await File.WriteAllBytesAsync(fileName, imageBytes);
        }
    }
}

